package cc.sjfx.sharedoc.dialog;

import java.util.Calendar;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import cc.sjfx.sharedoc.R;
import cc.sjfx.sharedoc.bean.MyDate;

public  class DatePickerDialog extends Dialog {
	private DatePicker picker;
	private Button submit;
	int year, monthOfYear, dayOfMonth;
	public DatePickerDialog(Context context,MyDate myDate) {
		super(context);
		setContentView(R.layout.dialog_datepicker);
		setTitle("选择日期");
		setCanceledOnTouchOutside(false);
		year = 0;
		monthOfYear = 0;
		dayOfMonth = 0;
		if(myDate!=null){
			year=myDate.getYear();
			monthOfYear=myDate.getMonth();
			dayOfMonth=myDate.getDay();
		}
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		picker = (DatePicker) findViewById(R.id.datePicker);
		submit = (Button) findViewById(R.id.submit);
		if (year == 0) {
			Calendar calendar = Calendar.getInstance();
			year = calendar.get(Calendar.YEAR);
			monthOfYear = calendar.get(Calendar.MONTH);
			dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);
		}
		picker.init(year, monthOfYear, dayOfMonth, new OnDateChangedListener() {
			@Override
			public void onDateChanged(DatePicker view, int year,
					int monthOfYear, int dayOfMonth) {
				DatePickerDialog.this.year = year;
				DatePickerDialog.this.monthOfYear = monthOfYear;
				DatePickerDialog.this.dayOfMonth = dayOfMonth;
			}

		});
	}
	public void getData(final OnDate date) {
		submit.setOnClickListener(new android.view.View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				date.getDate(year, monthOfYear + 1, dayOfMonth);
				dismiss();
			}
		});
	}
	public interface OnDate {
		public void getDate(int y, int m, int d);
	}
}
